package com.example.wly.application_3_homework;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.wly.application_3_homework.bean.WeaponInfo;

public class ShowInfoActivity extends AppCompatActivity {
    private TextView tvLife;
    private ProgressBar pbLife;
    private Button btnCoshop;
    private int PEQUEST_CODE=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_info);
        //1-获取上一页传递的用户信息，并展示
        //取出注册界面传递的数据
        //数据在intent中
        TextView tvName=(TextView)findViewById(R.id.tv_info_name);
        TextView tvPSW=(TextView)findViewById(R.id.tv_info_password);
        final Intent intent=getIntent();//获取注册界面传递的intent
        //根据关键字取出intent中的数据
        String name=intent.getStringExtra("username");
        String password=intent.getStringExtra("password");
        //Toast.makeText(this,"用户名和密码="+name+password,Toast.LENGTH_LONG).show();
        tvName.setText("用户名："+name);
        tvPSW.setText("密   码："+password);
        //2-初始化进度条和文本的值=0
        //在布局设计的时候已经初始化为0
        //3-点击按钮，跳转到商店界面------在商店购买武器

        /*Button btnGoshop=(Button)findViewById(R.id.btn_goshop);
        btnGoshop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(ShowInfoActivity.this,ShopActivity.class);
                startActivity(intent);

            }
        });*/
        Button btnGoShop=(Button) findViewById(R.id.btn_goshop);
        btnGoShop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1=new Intent(ShowInfoActivity.this,ShopActivity.class);
                //startActivity(intent1);
                //跳转到商店界面，从商店返回的时候，将武器的属性值带回
                //Intent intent1=new Intent(getApplicationContext(),ShopActivity.class);
                startActivityForResult(intent1,PEQUEST_CODE);//请求返回数据，请求码=1
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //4-从商店返回的时候，将数据的属性值带回
        WeaponInfo weaponInfo=(WeaponInfo)data.getSerializableExtra("weapon");

        //5-根据购买的武器，更新进度条和文本框的值
        tvLife=(TextView)findViewById(R.id.tv_info_life);
        pbLife=(ProgressBar)findViewById(R.id.pb_life);
        pbLife.setProgress(weaponInfo.getLife());
        tvLife.setText(""+weaponInfo.getLife());

    }
}
